package org;

import java.util.ArrayList;
//import java.util.Scanner;

import org.entities.LevelObject;
import org.newdawn.slick.AngelCodeFont;
import org.newdawn.slick.Color;
import org.newdawn.slick.Font;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.Input;
import org.newdawn.slick.Music;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.Sound;
import org.newdawn.slick.state.BasicGameState;
import org.newdawn.slick.state.StateBasedGame;
import org.newdawn.slick.state.transition.FadeInTransition;
import org.newdawn.slick.state.transition.FadeOutTransition;


/*
import java.io.BufferedReader;
import java.io.File; 
//import java.io.FileNotFoundException;
import java.io.FileWriter; 
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Writer;
import java.io.FileInputStream;
//import java.io.FileOutputStream; 
*/


public class TitleState  extends BasicGameState {

    private ArrayList <String>menus;
    private String start,control,exit,load,save,resume,info;
    private int selected;
    private StateBasedGame game;
    private Image logo,titlebg,screen;
    public static final int ID = 0;	
    private Font font;
    public static Music titleMusic;
    private Sound menuSelect;
    private boolean musicControl;

    public int getID() {
        return ID;
    }

    public void init(GameContainer container, StateBasedGame game) throws SlickException {

        this.game = game;		
        titlebg = new Image("data/image/titlebg.jpg");
        logo = new Image("data/image/sweaselLogo.png");
        //screen = new Image("data/anim/ctrlScreen.png");
        font = new AngelCodeFont("data/font/weasel.fnt","data/font/weasel.png");
        screen = new Image("data/image/ctrlScreen3.png");

        menus = new ArrayList<String>();	
        start   = "Start Game";
        control = "Options";
        load    = "Load n/a";
        save    = "Save n/a";
        info    = "Info";
        exit    = "Exit";
        menus.add(start);
        menus.add(control);
        menus.add(load);
        menus.add(save);
        menus.add(info);
        menus.add(exit);
        menuSelect=new Sound("data/sound/Click3.ogg");
        titleMusic=new Music(Progress.setMusic()); 
        Progress.showIntro=true;

        /*
        //==========================================================
        String path = System.getProperty("user.home") + "/test123/";
        new File(path).mkdir(); 
        try {    
            String var;
            String name;
            String value;
            BufferedReader reader;
            reader = new BufferedReader(new InputStreamReader(new FileInputStream(path + "chars.txt")));
            Scanner input = new Scanner(reader); 
            while(input.hasNextLine()) {
                var = input.nextLine();
                if(var != null && var.indexOf("=") != -1) {
                    name = var.substring(0, var.indexOf("="));
                    value = var.substring(var.indexOf("=") +1);
                    if (name.equals("shields"))
                        Progress.shields=Integer.parseInt(value);
                    System.out.printf("shieldsxxx=[%d]\n",Progress.shields);
                    System.out.printf("name=[%s], value=[%s]\n ",name.trim(),value.trim());

                } 
            }  
        }
        catch (Exception e) {
            e.printStackTrace();
        }
        */
    }



    public void update(GameContainer container, StateBasedGame game, int delta){


        if (!musicControl){
            musicControl=true;            
            titleMusic.loop();
        }
        if (!Progress.showIntro&&menus.contains(start)){
            menus.remove(start);
            resume   = "Resume";
            menus.add(0, resume);
        }        
    }

    public void render(GameContainer container, StateBasedGame game, Graphics g) {
        if (!LevelObject.debugMode)
            g.scale(2f, 2f);
        g.drawImage(titlebg,0,0);		
        g.drawImage(logo,200-(logo.getWidth()/2),50-(logo.getHeight()/2));
        g.drawImage(screen, 88, 135);
        g.scale(0.8f, 0.8f);
        g.setColor(Color.white);
        g.drawString(Progress.version, 420, 5);
        g.resetTransform(); 
        if (!LevelObject.debugMode)
            g.scale(2f, 2f);
        g.setFont(font);

        for (int i=0;i<menus.size();i++) {		

            if (selected == i) {
                g.setColor(Color.white);
                menus.get(i);
                g.drawString("[ "+menus.get(i)+" ]", 110, 155+i*18);
            }
            else{
                g.setColor(Color.gray);
                menus.get(i);
                g.drawString("  "+menus.get(i), 110, 155+i*18);
            }
        }
    }

    public void keyPressed(int key, char c) {
        if (key == Input.KEY_DOWN) {
            menuSelect.play(1,Progress.sfxVol/20);
            selected++;
            if (selected >= menus.size()) {
                selected = 0;
            }
        }
        if (key == Input.KEY_UP) {
            menuSelect.play(1,Progress.sfxVol/20);
            selected--;
            if (selected < 0) {
                selected = menus.size()-1;
            }
        }

    }


    public void keyReleased(int key, char c) {

        if (key == Input.KEY_ENTER) {
            if (selected==0){
                musicControl=false;
                titleMusic.stop();
                if (Progress.showIntro)
                    game.enterState(IntroState.ID, new FadeOutTransition(Color.black), new FadeInTransition(Color.black));
                else
                    game.enterState(GameState.ID, new FadeOutTransition(Color.black), new FadeInTransition(Color.black));
            }            
            if (selected==1)
                game.enterState(CtrlState.ID, new FadeOutTransition(Color.black), new FadeInTransition(Color.black));
            if (selected==2)
                game.enterState(CtrlState.ID, new FadeOutTransition(Color.black), new FadeInTransition(Color.black));
            if (selected==3)                
                game.enterState(CtrlState.ID, new FadeOutTransition(Color.black), new FadeInTransition(Color.black));
            if (selected==4)                
                game.enterState(InfoState.ID, new FadeOutTransition(Color.black), new FadeInTransition(Color.black));
            if (selected==5){System.exit(0);}
        }
    }
}
